What is web-streams-polyfill?
The web-streams-polyfill package is a polyfill for the Streams API, which provides a standard interface for representing and handling a sequence of data in JavaScript. This polyfill allows developers to use the Streams API in environments where it is not natively supported.
What are web-streams-polyfill's main functionalities?
ReadableStream
This feature allows you to create a stream of data that can be read in chunks. The code sample demonstrates how to create a simple ReadableStream that enqueues two chunks of data and then closes the stream.
const readableStream = new ReadableStream({
start(controller) {
controller.enqueue('Hello, ');
controller.enqueue('World!');
controller.close();
}
});
readableStream.getReader().read().then(({ value, done }) => {
if (!done) console.log(value);
});
WritableStream
This feature allows you to create a stream where data can be written. The code sample shows how to create a WritableStream and write a chunk of data to it, then close the stream.
const writableStream = new WritableStream({
write(chunk) {
console.log(chunk);
},
close() {
console.log('Stream closed');
}
});
const writer = writableStream.getWriter();
writer.write('Hello, World!').then(() => writer.close());
TransformStream
This feature allows you to create a stream that transforms data as it passes through. The code sample demonstrates how to create a TransformStream that converts chunks of text to uppercase and then pipes data from a ReadableStream through it to a WritableStream.
const uppercaseTransform = new TransformStream({
transform(chunk, controller) {
controller.enqueue(chunk.toUpperCase());
}
});
const readable = new ReadableStream({
start(controller) {
controller.enqueue('hello');
controller.enqueue('world');
controller.close();
}
});
const writable = new WritableStream({
write(chunk) {
console.log(chunk);
}
});
readable.pipeThrough(uppercaseTransform).pipeTo(writable);
Other packages similar to web-streams-polyfill
stream-browserify
This package is a browserify version of the Node.js core streams module. It provides similar functionality for handling streams of data but is designed to mimic Node.js streams specifically, rather than implementing the Web Streams API.
readable-stream
This package is a mirror of the Streams API from Node.js for use in browsers and other environments. It is similar to web-streams-polyfill but focuses on Node.js compatibility and may have differences in API and behavior compared to the Web Streams API.
blob-polyfill
While not a direct alternative to web-streams-polyfill, blob-polyfill provides Blob support in browsers that do not natively support the Blob constructor. It can be used in conjunction with streams to handle binary data.
web-streams-polyfill
Web Streams, based on the WHATWG spec reference implementation.
Links
Usage
This library comes in multiple variants:
web-streams-polyfill
: a polyfill that replaces the native stream implementations.
Recommended for use in web apps supporting older browsers through a <script>
tag.web-streams-polyfill/es6
: a polyfill targeting ES2015+ environments.
Recommended for use in web apps supporting modern browsers through a <script>
tag.web-streams-polyfill/es2018
: a polyfill targeting ES2018+ environments.web-streams-polyfill/ponyfill
: a ponyfill that provides
the stream implementations without replacing any globals.
Recommended for use in legacy Node applications, or in web libraries supporting older browsers.web-streams-polyfill/ponyfill/es6
: a ponyfill targeting ES2015+ environments.
Recommended for use in Node 6+ applications, or in web libraries supporting modern browsers.web-streams-polyfill/ponyfill/es2018
: a ponyfill targeting ES2018+ environments.
Recommended for use in Node 10+ applications.
Each variant also includes TypeScript type definitions, compatible with the DOM type definitions for streams included in TypeScript.
Usage as a polyfill:
<script src="https://unpkg.com/web-streams-polyfill/dist/polyfill.min.js"></script>
<script src="/path/to/web-streams-polyfill/dist/polyfill.min.js"></script>
<script>
var readable = new ReadableStream();
</script>
Usage as a Node module:
var streams = require("web-streams-polyfill/ponyfill");
var readable = new streams.ReadableStream();
Usage as a ES2015 module:
import { ReadableStream } from "web-streams-polyfill/ponyfill";
const readable = new ReadableStream();
Compatibility
The polyfill
and ponyfill
variants work in any ES5-compatible environment that has a global Promise
.
If you need to support older browsers or Node versions that do not have a native Promise
implementation
(check the support table), you must first include a Promise
polyfill
(e.g. promise-polyfill).
The polyfill/es6
and ponyfill/es6
variants work in any ES2015-compatible environment.
The polyfill/es2018
and ponyfill/es2018
variants work in any ES2018-compatible environment.
Async iterable support for ReadableStream
is available in all variants, but requires an ES2018-compatible environment or a polyfill for Symbol.asyncIterator
.
Compliance
The polyfill implements version ed00d2f
(17 Feb 2020) of the streams specification.
The polyfill is tested against the same web platform tests that are used by browsers to test their native implementations.
The polyfill aims to pass all tests, although it allows some exceptions for practical reasons:
- The
es2018
variant passes all of the tests, except for the detached buffer tests for readable byte streams.
These tests require the implementation to synchronously transfer the contents of an ArrayBuffer
, which is not yet possible from JavaScript (although there is a proposal to make it possible).
The reference implementation "cheats" on these tests by making a copy instead, but that is unacceptable for the polyfill's performance (#3). - The
es6
variant passes the same tests as the es2018
variant, except for the test for the prototype of ReadableStream
's async iterator.
Retrieving the correct %AsyncIteratorPrototype%
requires using an async generator (async function* () {}
), which is invalid syntax before ES2018.
Instead, the polyfill creates its own version which is functionally equivalent to the real prototype. - The
es5
variant passes the same tests as the es6
variant, except for various tests about specific characteristics of the constructors, properties and methods.
These test failures do not affect the run-time behavior of the polyfill.
For example:
- The
name
property of down-leveled constructors is incorrect. - The
length
property of down-leveled constructors and methods with optional arguments is incorrect. - Not all properties and methods are correctly marked as non-enumerable.
- Down-leveled class methods are not correctly marked as non-constructable.
The type definitions are compatible with the built-in stream types of TypeScript 3.3.
Contributors
Thanks to these people for their work on the original polyfill: